package com.medical.medical.controller.gyncontroller;

import com.alibaba.fastjson.JSONArray;
import com.medical.medical.entity.Notice;
import com.medical.medical.service.NoticeService;
import com.medical.medical.util.Constants;
import com.medical.medical.util.PageSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @authorDesc 你相信光吗？
 * @author GUOYN
 * @date 2022-09-15 08:41:26
 * @version 1.0.0
 * @description 系统更新公告请求层
 */
@Slf4j
@Controller
public class GYNNoticeController {
    @Resource
    private NoticeService noticeService;
    @Resource
    private Notice notice;
    /**
      * @description 根据条件查询系统更新公告
      * @author GUOYN
      * @date 2022-09-15 10:36:32  * @param startTime
     * @param terminationTime
     * @param currentPage
     * @param noticeType
     * @param model
      * @return {@link String}
      */
    @RequestMapping("systemnoticelist")
    public String getSystemNoticeList(@RequestParam(required = false) Date startTime,
                                            @RequestParam(required = false) Date terminationTime,
                                            @RequestParam(required = false) String currentPage,
                                            int noticeType, Model model) {
        /*log.info("startTime1={}",startTime1);
        log.info("terminationTime1={}",terminationTime1);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date startTime=null;
        Date terminationTime=null;
        if (startTime1!=null && terminationTime1!=null){
            try {
                startTime = simpleFormat.parse(startTime1);
                terminationTime= simpleFormat.parse(terminationTime1);
            } catch (ParseException e) {
                e.printStackTrace();
                log.info("解析当前页时发生异常");
            }
        }
        log.info("startTime={}",startTime);
        log.info("terminationTime={}",terminationTime);
*/
        log.info("------开始查询------");
        //获得当前页码
        int current = 0;
        if(currentPage != null){
            try{
                current = Integer.valueOf(currentPage);
            }catch(NumberFormatException e){
                e.printStackTrace();
                log.info("解析当前页时发生异常");
            }
        }
        //总数量（表）
        int totalCount	= noticeService.getNoticeCount(startTime,terminationTime,noticeType);
        if(totalCount == 0){
            model.addAttribute("totalCountFlag",1);
            model.addAttribute("startTime",startTime);
            model.addAttribute("terminationTime",terminationTime);
            log.info("查询不到系统更新公告！");
            model.addAttribute("result","查询不到此日期段的系统更新公告！");
            return "/admin/systemnoticelist";
        }
        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(current);
        pages.setPageSize(Constants.PAGE_SIZE);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if(current < 1){
            current = 1;
        }else if(current > totalPageCount) {
            current = totalPageCount;
        }
        List<Notice> noticeList =noticeService.getSystemNoticeList(startTime,terminationTime,noticeType,current);
        if (noticeList!=null && noticeList.isEmpty()){
            log.info("获得两个日期之间的数据失败！");
            model.addAttribute("noticeListFlag",1);
            model.addAttribute("result","查询不到此日期段的系统更新公告！请重新查询！");
        }else {
            log.info("获得两个日期之间的数据成功！");
            model.addAttribute("noticeList",noticeList);
        }
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("startTime",startTime);
        model.addAttribute("terminationTime",terminationTime);
        model.addAttribute("current",current);
        model.addAttribute("totalPageCount",totalPageCount);
        return "/admin/systemnoticelist";
    }
    /**
      * @description 根据id获得当前系统更新公告
      * @author GUOYN
      * @date 2022-09-15 12:07:28  * @param id
     * @param model
      * @return {@link Notice}
      */
    @RequestMapping("systemnoticeview")
    public String getSystemNoticeView(int noticeId, int noticeType, Model model){
        Notice notice = noticeService.getSystemNoticeView(noticeId,noticeType);
        if (notice!=null){
            log.info("获得当前系统更新公告成功！");
            model.addAttribute("notice",notice);
        }else {
            log.info("获得当前系统更新公告失败！");
            model.addAttribute("result","获得当前系统更新公告失败!");
        }
        return "/admin/systemnoticeview";
    }

    @RequestMapping("tosystemnoticeadd")
    public String toaddSystemNotice(){
        return "/admin/systemnoticeadd";
    }
    /**
      * @description 添加系统更新公告
      * @author GUOYN
      * @date 2022-09-15 14:33:17  * @param notice
     * @param session
      * @return {@link String}
      */
    @RequestMapping("systemnoticeadd")
    public String addSystemNotice(Notice notice, HttpSession session,Model model){
        notice.setNoticeState("未公开");
        notice.setNoticeType(1);
        boolean flag = noticeService.addSystemNotice(notice);
        if (flag){
            log.info("添加系统更新公告成功!");
            return "redirect:systemnoticelist?noticeType="+notice.getNoticeType();
        }else {
            model.addAttribute("result","添加系统更新公告失败！请重新添加！");
            return "systemnoticeadd";
        }
    }
    /**
      * @description 修改系统更新公告（发布状态/发布时间）
      * @author GUOYN
      * @date 2022-09-15 15:10:03  * @param noticeId
     * @param model
      * @return {@link String}
      */
    @RequestMapping("systemnoticeupdate")
    public String updateSystemNotice(int noticeId, int noticeType,Model model,HttpSession session){
        log.info("{}",noticeId);
        notice.setNoticeId(noticeId);
        notice.setNoticeState("公开");
        notice.setOutTime(new Date());
        log.info("{}",notice);
        boolean flag = noticeService.updateSystemNotice(notice);
        if (flag){
            log.info("修改系统更新公告（发布状态/发布时间）成功！");
            //model.addAttribute("flag",1);
            //model.addAttribute("result","发布公告成功！");
        }else {
            log.info("修改系统更新公告（发布状态/发布时间）成功！");
            //model.addAttribute("flag",0);
            session.setAttribute("result","发布公告失败！");
        }
        return "redirect:systemnoticelist?noticeType="+noticeType;
    }

    /**
     * @description 转换日期
     * @author GUOYN
     * @date 2022-08-27 19:41:20  * @param binder
     * @return
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    /**
      * @description 获得最新公告信息
      * @author GUOYN
      * @date 2022-09-26 11:18:59  * @param
      * @return {@link Object}
      */
    @RequestMapping("newsystemnoticeview")
    @ResponseBody
    public Object getNewSystemNoticeView(){
        Notice notice = noticeService.getNewSystemNoticeView();
        log.info("{}",notice);
        return JSONArray.toJSONString(notice);
    }
}
